﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.Core.IService;
using Empires.CRL.DataContract;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.Core.Implement
{
    //在线咨询
    public class CompanyOnlineService : ICompanyOnlineService
    {
        public List<DefaultDataPack> GetOnlineConsultation(string copname, int page, int size)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            int itemCount = access.GetOnlineConsulattionItemCount(copname);
            return CompanyHelper.InjuectPagingInfo((currentPage) =>
            {
                return access.GetOnlineConsultation(copname, currentPage, size);
            },
            itemCount, page, size);

            //int pagecount = itemCount % size == 0 ? itemCount / size : itemCount / size + 1;
            //page = page < 0 ? 0 : page;
            //page = page > pagecount - 1 ? pagecount - 1 : page;
            //int previouspage = page == 0 ? 0 : page - 1;
            //int nextpage = page == pagecount - 1 ? pagecount - 1 : page + 1;
            //var list = access.GetOnlineConsultation(copname, page, size);
            //BLL.ServerProvider.RoleCheckProvider.SetAuthInfo(ref list, copname);

            //return list.Select(c =>
            //{
            //    c.AUTHOR = String.IsNullOrEmpty(c.AUTHOR) ? "" : c.AUTHOR.Length > 10 ? c.AUTHOR.Substring(0, 10) : c.AUTHOR;
            //    c.EXTFIELD2 = String.IsNullOrEmpty(c.EXTFIELD2) ? "" : c.EXTFIELD2.Length > 10 ? c.EXTFIELD2.Substring(0, 10) : c.EXTFIELD2;
            //    c.ITEMCOUNT = itemCount;
            //    c.PAGESIZE = size;
            //    c.CURRENTPAGE = page + 1;
            //    c.PAGECOUNT = pagecount;
            //    c.PREVIOUSPAGE = previouspage;
            //    c.NEXTPAGE = nextpage;
            //    c.FIRSTPAGE = 0;
            //    c.LASTPAGE = pagecount - 1;
            //    return c;
            //}).ToList();
        }

        public List<DefaultDataPack> GetOnlineConsultationForIndex(string copname, int size)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            return access.GetOnlineConsultationForIndex(copname, size);
        }

        public int PostOnlineConsultation(string copname, string Author, string Content)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            return access.PostOnlineConsultation(copname, Author, Content);
        }

        public int PostOnlineReply(string copname, int mid, string author, string content, string phone)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            return access.PostOnlineReply(copname, mid, author, content, phone);
        }

        public int DeleteOnlineItem(string copname, int mid)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            return access.DeleteOnlineItem(copname, mid);
        }

        public List<DefaultDataPack> GetOnlineConsultationItem(string copname, int mid)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            return access.GetOnlineConsultationItem(copname, mid).Select(c =>
            {
                c.AUTHOR = String.IsNullOrEmpty(c.AUTHOR) ? "" : c.AUTHOR.Length > 10 ? c.AUTHOR.Substring(0, 10) : c.AUTHOR;
                c.EXTFIELD2 = String.IsNullOrEmpty(c.EXTFIELD2) ? "" : c.EXTFIELD2.Length > 10 ? c.EXTFIELD2.Substring(0, 10) : c.EXTFIELD2;
                return c;
            }).ToList();
        }

        public bool EditOnlineConsultaion(string copname, OnlineDataPack onlineDataPack)
        {
            RAL.Access.ICompanyOnlineAccess access = new RAL.Pump.CompanyOnlineDataPump();
            return access.EditOnlineConsultaion(copname, onlineDataPack);
        }
    }
}
